using System.Collections.Generic;
using System.Text;
using OracleInternal.Common;

namespace OracleInternal.SqlAndPlsqlParser.LocalParsing
{
	public sealed class OracleLpSelectStatement : OracleLpStatement, IOracleLpColumnDescriptorContainer, IOracleLpNamedObjectContainer
	{
		private OracleLpSubquery m_vSubquery;

		private List<OracleLpColumnDescriptor> m_vColumnDescriptors;

		internal OracleLpSubquery Subquery
		{
			get
			{
				return m_vSubquery;
			}
			set
			{
				m_vSubquery = value;
				if (m_vSubquery != null)
				{
					m_vSubquery.Parent = this;
				}
			}
		}

		public override OracleLpStatementType StatementType => OracleLpStatementType.Select;

		public List<OracleLpColumnDescriptor> ColumnDescriptors
		{
			get
			{
				if (m_vColumnDescriptors == null)
				{
					Resolve();
				}
				return m_vColumnDescriptors;
			}
		}

		internal OracleLpSelectStatement(OracleLpTextFragment text, IOracleMetadata odpContext)
			: base(text, odpContext)
		{
		}

		public void Resolve()
		{
			if (m_vNamedObjectsReferences == null)
			{
				m_vNamedObjectsReferences = new List<OracleLpQteNamedObject>();
				RetrieveNamedObjectReferences(this);
				m_vColumnDescriptors = m_vSubquery.ColumnDescriptors;
			}
		}

		public void RetrieveNamedObjectReferences(OracleLpStatement statement)
		{
			m_vSubquery.RetrieveNamedObjectReferences(this);
			if (m_vNamedObjectsReferences.Count == 0)
			{
				return;
			}
			List<OracleLpTable> list = new List<OracleLpTable>();
			foreach (OracleLpQteNamedObject vNamedObjectsReference in m_vNamedObjectsReferences)
			{
				list.Add(new OracleLpTable((vNamedObjectsReference.SchemaName == null) ? null : vNamedObjectsReference.SchemaName.DbName, (vNamedObjectsReference.ObjectName == null) ? null : vNamedObjectsReference.ObjectName.DbName, (vNamedObjectsReference.Dblink == null) ? null : vNamedObjectsReference.Dblink.ToString()));
			}
			List<OracleLpQteNamedObject>.Enumerator enumerator2 = m_vNamedObjectsReferences.GetEnumerator();
			enumerator2.MoveNext();
			foreach (OracleLpTableColumns item in m_vODPContext.GetColumnInformation(list))
			{
				enumerator2.Current.RetrieveColumnsInformation(item);
				enumerator2.MoveNext();
			}
		}

		internal override void ToString(StringBuilder sb)
		{
			base.ToString(sb);
			m_vSubquery.ToString(sb);
			sb.Append('\n');
			sb.Append("Column descriptors\n");
			sb.Append("==================\n");
			ColumnDescriptors.ForEach(delegate(OracleLpColumnDescriptor t)
			{
				sb.Append(t.ToString());
			});
			sb.Append("\n\n");
		}
	}
}
